home *** CD-ROM | disk | FTP | other *** search
- Path: calvin.risq.qc.ca!news
- From: pcoulomb@criq.qc.ca (Pierre Coulombe)
- Newsgroups: comp.lang.c
- Subject: Type casting
- Date: 21 Feb 1996 18:18:58 GMT
- Organization: CRIQ
- Message-ID: <4gfnmi$gsc@calvin.risq.qc.ca>
- NNTP-Posting-Host: pcoulomb.criq.qc.ca
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.6
-
- I have a problem with type casting in Visual C 1.5.
- I expected the following program to print the value 254 for valI.
- Instead it gives the output shown below.
- Can someone tell me where is my mistake ?
-
-
- ******** Program *********
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- typedef unsigned int UINT;
-
- #define MM_TO_UNITS 10.0F
-
- void main(void)
- {
- float valF;
- UINT valI;
- char string[80];
-
- strcpy(string, "25.4");
-
- valI = (UINT) ((float) atof(string) * MM_TO_UNITS);
- printf("valI = %u\n", valI);
-
- sprintf(string, "%f", (float) atof(string) * MM_TO_UNITS);
- printf("string = %s\n", string);
-
- strcpy(string, "25.4");
- sprintf(string, "%u", (UINT) ((float) atof(string) * MM_TO_UNITS));
- printf("string = %s\n", string);
- }
-
-
- ******** Output *********
-
- valI = 253
- string = 254.000000
- string = 253
-
-
-
-
-